<template>
  <view class="empty-pallet-container">
    <!-- <uni-nav-bar title="空托处理" left-icon="back" @clickLeft="goBack" :border="false"></uni-nav-bar> -->

    <view class="form-section">
      <view class="form-item">
        <text class="label">地码:</text>
        <input
          v-model="formData.locationCode"
          class="input"
          type="text"
          placeholder="请输入地码"
        />
      </view>
	  <view class="button-row">
	    <button @click="emptyPlatformOccupation" class="btn btn-primary left-btn" :disabled="loading1">
	      <!-- 空箱要货 -->
	      <text v-if="loading1" class="cuIcon-loading2 iconfont-spin"></text>
	      {{ loading1 ? '提交中...' : '空托占用' }}
	    </button>
	    <button @click="AirBracketSupplement" class="btn btn-primary right-btn" :disabled="loading3">
	      <!-- 回库 -->
	      <text v-if="loading3" class="cuIcon-loading2 iconfont-spin"></text>
	      {{ loading3 ? '提交中...' : '空托补充' }}
	    </button>
	  </view>
    </view>

<!--    <view class="button-section">
      <button class="btn btn-primary" @click="handleOccupyEmptyPallet">空托占用</button>
      <button class="btn btn-danger" @click="handleCallEmptyPallet">呼叫空托</button>
      <button class="btn btn-primary" @click="handleReplenishEmptyPallet">空托补充</button>
    </view> -->
  </view>
</template>

<script>
import {emptyPlatOccupation,callSpaceTrust,AirBracketSupplement} from '@/api/system/emptyPallet';

var main, receiver, filter; // 扫码相关变量
export default {
  data() {
    return {
		formData: {
      locationCode: '', 
	  },// 地码输入框的值
	   loading1: false,
	   loading2: false,
	   loading3: false
    };
  },
  
  onLoad() {
    this.initScan();
  },
  
  onShow() {
    this.startScan();
  },
  
  onHide() {
    this.stopScan();
  },
  
  onUnload() {
    this.stopScan();
  },
  methods: {
	  
	  initScan() {
	    let _this = this;
      /* #ifdef APP-PLUS */
	    main = plus.android.runtimeMainActivity();
	    var IntentFilter = plus.android.importClass('android.content.IntentFilter');
	    filter = new IntentFilter();
	    filter.addAction("com.service.scanner.data")
	    receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
	      onReceive: function (context, intent) {
	        plus.android.importClass(intent);
	        let code = intent.getStringExtra("ScanCode");
	        console.log(code)
	        _this.handleScanResult(code);
	      }
	    });
      /* #endif */
	  },
	  
	  startScan() {
	    /* #ifdef APP-PLUS */
	    main.registerReceiver(receiver, filter);
	    /* #endif */
	  },
	  
	  stopScan() {
	    /* #ifdef APP-PLUS */
	    main.unregisterReceiver(receiver);
	    /* #endif */
	  },
	  
	  handleScanResult(code) {
	    // 判断扫码内容类型
	    if (code.startsWith('L')) {
	      // 库位格式
	      if (!this.formData.frmStation) {
	        this.formData.frmStation = code.substring(1);
	      } else if (!this.formData.toLocation) {
	        this.formData.toLocation = code.substring(1);
	      }
	    } else if (code.startsWith('M')) {
	      // 物料编号格式
	      this.formData.materialCode = code.substring(1);
	    } else {
	      // 默认当作物料编号处理
	      this.formData.materialCode = code;
	    }
	  },
	  // 重置表单
	  	handleReset(){
	  		 this.formData = {
	  			   locationCode: '', 
	  			 };
	  	},
    goBack() {
      uni.navigateBack(); // 返回上一页
    },
    emptyPlatformOccupation() {
      // TODO: 实现空托占用逻辑
      console.log('空托占用:', this.formData);


		if (!this.formData.locationCode) {
		  this.$modal.msgError('请输入地码');
		  return;
		}
		 this.loading1 = true;
		 emptyPlatOccupation(this.formData)
		  .then(res => {
		 console.log(this.formData+"songmuyan")
		 if (res.code === 200) {
			this.loading1=false,
		   this.$modal.msgSuccess('任务成功');
		   this.handleReset();
		 } else {
		   this.$modal.msgError(res.msg || '任务失败');
		 }
		 })
		 .catch(error => {
		   console.error('操作失败:', error);
		   this.$modal.msgError('操作失败，请稍后重试');
		 })
		 .finally(() => {
		   this.loading1 = false;
		 });
		 
    },
    callSpaceTrust() {
      // TODO: 实现呼叫空托逻辑
      console.log('呼叫空托:', this.locationCode);
	
	  this.handleReset();
	  	if (this.loading2) return;
	  		if (!this.formData.locationCode) {
	  		  this.$modal.msgError('请输入地码');
	  		  return;
	  		}
	  		 this.loading2 = true;
	  		 callSpaceTrust(this.formData)
	  		  .then(res => {
	  		 console.log(res+"songmuyan")
	  		 if (res.code === 200) {
	  		   this.$modal.msgSuccess('操作成功');
	  		   this.handleReset();
	  		 } else {
	  		   this.$modal.msgError(res.msg || '操作失败');
	  		 }
	  		 })
	  		 .catch(error => {
	  		   console.error('操作失败:', error);
	  		   this.$modal.msgError('操作失败，请稍后重试');
	  		 })
	  		 .finally(() => {
	  		   this.loading = false;
	  		 });
    },
    AirBracketSupplement() {
      // TODO: 实现空托补充逻辑
      console.log('空托补充:', this.locationCode);
	
	 	if (this.loading3) return;
	 		if (!this.formData.locationCode) {
	 		  this.$modal.msgError('请输入地码');
	 		  return;
	 		}
	 		 this.loading3 = true;
	 		 AirBracketSupplement(this.formData)
	 		  .then(res => {
	 		 if (res.code === 200) {
         this.loading3=false,
	 		   this.$modal.msgSuccess('操作成功');
	 		   this.handleReset();
	 		 } else {
	 		   this.$modal.msgError(res.msg || '操作失败');
	 		 }
	 		 })
	 		 .catch(error => {
	 		   console.error('操作失败:', error);
	 		   this.$modal.msgError('操作失败，请稍后重试');
	 		 })
	 		 .finally(() => {
	 		   this.loading3 = false;
	 		 });
    },
  },
};
</script>

<style lang="scss" scoped>
@import '@/uni_modules/uni-scss/variables.scss';

.empty-pallet-container {
  padding: 16px;
}

.form-section {
  margin-bottom: 20px;
  background-color: $uni-white; // 使用 uni-scss 变量
  border-radius: 8px;
  padding: 16px;

  display: flex;
  flex-direction: column;

  .form-item {
    margin-bottom: 16px; // 调整间距

    .label {
      display: block; // 标签独占一行
      font-size: 14px;
      color: $uni-main-color; // 使用 uni-scss 变量
      margin-bottom: 8px; // 调整标签与输入框间距
      font-weight: 500; // 加粗
    }

    .input {
      width: 100%; // 输入框宽度100%
      height: 40px; // 调整高度
      background-color: $uni-bg-color; // 使用 uni-scss 变量
      border-radius: 4px;
      padding: 0 12px; // 调整内边距
      font-size: 14px;
      color: $uni-main-color; // 使用 uni-scss 变量
      border: none; // 移除边框
    }
  }

  .button-row {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    margin-top: 12px;
  }
  .left-btn,
  .right-btn {
    width: 49%;
    margin: 0;
  }
  .left-btn {
    margin-right: 2%;
  }
  .right-btn {
    margin-left: 0;
  }
}

.button-section {
  display: flex;
  justify-content: space-between; // 按钮左右对齐
  margin-top: 24px; // 调整上边距

  .btn {
    width: 48%; // 按钮宽度
    height: 40px;
    border-radius: 4px;
    font-size: 14px;
    font-weight: 500;
    text-align: center;
    line-height: 40px; // 垂直居中文字

    &.btn-primary {
      background-color: $uni-primary; // 使用 uni-scss 变量
      color: $uni-white; // 使用 uni-scss 变量
    }

    &.btn-danger {
      background-color: $uni-error; // 使用 uni-scss 变量
      color: $uni-white; // 使用 uni-scss 变量
    }
  }
}

.inventory-counting-container {
  padding: 16px;
  // 其他样式
}

.task-section,
.scanning-section,
.difference-section,
.submit-section {
  margin-bottom: 20px;
  padding: 16px;
  background-color: #fff;
  border-radius: 8px;
  // 其他样式
}

.section-title {
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 16px;
  display: block;
}

.form-item {
  margin-bottom: 12px;

  .label {
    display: block;
    font-size: 14px;
    color: #333;
    margin-bottom: 4px;
  }

  .input {
    width: 100%;
    height: 36px;
    border: 1px solid #eee;
    border-radius: 4px;
    padding: 0 8px;
    font-size: 14px;
  }
}

.btn-primary {
  background-color: #4A90E2;
  color: #fff;
  height: 40px;
  line-height: 40px;
  font-size: 14px;
  border-radius: 4px;
  text-align: center;
  margin-top: 12px;
}

.scan-result {
  margin-bottom: 12px;
  font-size: 14px;
  color: #333;
  display: flex;
  align-items: center;

  .iconfont-spin {
    margin-left: 8px;
  }
}

.scanned-items-list {
  margin-top: 16px;
  border: 1px solid #eee;
  border-radius: 4px;
  overflow: hidden;

  .item-header, .scanned-item {
    display: flex;
    padding: 8px;
    border-bottom: 1px solid #eee;
    font-size: 12px;
    text-align: center;
    align-items: center; // 垂直居中

    .item-code { width: 25%; }
    .item-info { width: 30%; }
    .item-stock { width: 15%; }
    .item-stock.difference { color: #ff4d4f; font-weight: bold;}
    .item-stock.difference.is-different { color: #ff4d4f; font-weight: bold;} // 明确差异颜色

    .input-counted {
      width: 100%; // 让输入框填充父容器
      height: 24px; // 调整输入框高度
      border: 1px solid #ccc;
      border-radius: 4px;
      padding: 0 4px;
      font-size: 12px;
      text-align: center;
    }
  }

  .item-header {
    background-color: #f8f9fa;
    font-weight: bold;
    color: #333;
  }

  .scanned-item {
    background-color: #fff;
    &:last-child {
      border-bottom: none;
    }
  }

   .empty-state {
    padding: 16px;
    text-align: center;
    font-size: 14px;
    color: #999;
    background-color: #fff;
  }
}

.iconfont-spin {
  display: inline-block;
  animation: loading-rotate 2s linear infinite;
}

@keyframes loading-rotate {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}
</style>